package com.csdj.springboottest.controller;

import com.csdj.springboottest.common.WebResult;
import com.csdj.springboottest.entity.Config;
import com.csdj.springboottest.entity.User;
import com.csdj.springboottest.service.ConfigService;
import com.csdj.springboottest.service.RedisService;
import com.csdj.springboottest.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Locale;

/**
 * author:cloud
 * date:2021/4/16 0016
 **/
@RestController
@RequestMapping("/test")
public class TestController {

    private Logger log = LoggerFactory.getLogger("controller");

    @Resource
    private UserService userService;
    @Resource
    private RedisService redisService;
    @Resource
    private ConfigService configService;

//    @Resource
//    private SayHello sayHello;

    @GetMapping("/index")
    public String test(){
        return "good";
    }


    @GetMapping("/auto")
    public String auto(){
//        sayHello.say();
        return "good";
    }

    @GetMapping("/log")
    public String log(String param){

        log.info("我要打印日志 参数是：{} 参数打印完成{}",param,param);
        return "log";
    }


    @GetMapping("/list")
    public WebResult result(){
        List<User> list = userService.getAll();
        final WebResult result = new WebResult();
        result.setCode(1);
        result.putData("data",list);
        return result;
    }


    @GetMapping("/redis")
    public WebResult reids(int id){
        User user = userService.get(id);
        redisService.set("user",user,100);
        redisService.setHash("hash","cloud",user);
        final WebResult webResult = new WebResult(1);
        webResult.putData("data",user);
        return webResult;
    }

    @GetMapping("/redis/str")
    public WebResult redisString(String str){
        redisService.set("key",str,100);
        redisService.publish("cloud","this is a test msg");
        return new WebResult(1);

    }

    @GetMapping("/config/{type}")
    public WebResult config(@PathVariable("type") String type){
        List<Config> list = configService.getByType(type);
        final WebResult webResult = new WebResult(200);
        webResult.putData("data",list);
        return webResult;
    }


    @GetMapping("/user/{username}")
    public WebResult username(@PathVariable("username")String username){
        User user = userService.getByUsername(username);
        final WebResult result = new WebResult(200);
        result.putData("user",user);
        return result;
    }

    @GetMapping("/lang")
    public WebResult langTest(String lang){

        switch (lang){
            case "zh":
                return new WebResult(401, Locale.CHINA);
            case "en":
                return new WebResult(401, Locale.US);
            default:
                return new WebResult(401);
        }
    }


    @GetMapping("/config/{type}/{key}")
    public WebResult getConfig(@PathVariable("type") String type,@PathVariable("key")String key){
        Config config = configService.get(type, key);
        return new WebResult(200).putData("data",config);
    }

    @PostMapping("/config")
    public WebResult saveConfig(Config config){
        configService.insert(config);
        return new WebResult(200);
    }

    @GetMapping("/user")
    public WebResult getUser(int id){
        return new WebResult(200).putData("data",userService.get(id));
    }
}
